﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SqlQueryAnalyzer
{
    public partial class ServerConnectForm : BaseForm
    {
        Dictionary<string, AutoComplete> autoDict;

        public ComboBox DDLFunctioin
        {
            get { return this.ddlFunction; }
        }
        public ServerConnectForm()
        {
            InitializeComponent();
            Util.SetTilte(this);
        }

        private void ServerConnectForm_Load(object sender, EventArgs e)
        {
            try
            {
                this.ddlFunction.SelectedIndex = 0;
                autoDict = AutoComplete.GetAutoCompleteSource();
                AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
                auto.AddRange(autoDict.Values.Select(c => c.Server).ToArray());
                this.txtServer.AutoCompleteCustomSource = auto;
                this.ddlAuthenticationType.SelectedIndex = 1;

            }
            catch (Exception ex)
            {
                MessageBox.Show("自动完成数据初始化异常:" + ex.GetBaseException().Message);
            }

        }

        private void ddlAuthenticationType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlAuthenticationType.SelectedIndex == 0)
            {
                this.lblUserName.Enabled = false;
                this.lblPassword.Enabled = false;
                this.txtUserName.Enabled = false;
                this.txtPassword.Enabled = false;
            }
            else
            {
                this.lblUserName.Enabled = true;
                this.lblPassword.Enabled = true;
                this.txtUserName.Enabled = true;
                this.txtPassword.Enabled = true;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (InputValid())
            {
                string connString = GetConnectionString();
                if (TestConnection(connString))
                {
                    if (cbxRememberPassword.Checked)
                    {
                        try
                        {
                            if (!autoDict.ContainsKey(this.txtServer.Text.ToLower()))
                            {
                                AutoComplete.SaveAutoCompleteSource(this.txtServer.Text,this.txtUserName.Text,this.txtPassword.Text);
                            }                           
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("保存自动完成数据异常:" + ex.GetBaseException().Message);
                        }
                    }
                    Util.ConnectionString = connString;
                    Util.Server = this.txtServer.Text;
                    this.DialogResult = DialogResult.OK;
                }
            }

        }

        public string GetConnectionString()
        {
            string server = this.txtServer.Text.Trim();
            string username = this.txtUserName.Text.Trim();
            string password = this.txtPassword.Text;
            string connString = "";

            if (this.ddlAuthenticationType.SelectedIndex == 0)
            {
                connString = string.Format("Data Source={0};Integrated Security=true;Initial Catalog=master", server);
            }
            else
            {
                connString = string.Format("Data Source={0};Initial Catalog=master;User ID={1};Password={2};", server, username, password);
            }
            return connString;
        }

        public bool InputValid()
        {
            string server = this.txtServer.Text.Trim();
            string username = this.txtUserName.Text.Trim();
            string password = this.txtPassword.Text;
            if (string.IsNullOrEmpty(server))
            {
                MessageBox.Show("请输入服务器");
                return false;
            }
            if (this.ddlAuthenticationType.SelectedIndex != 0)
            {
                if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
                {
                    MessageBox.Show("请输入用户名、密码");
                    return false;
                }
            }
            return true;
        }

        public bool TestConnection(string connString)
        {
            try
            {
                SqlConnection conn = new SqlConnection(connString);
                conn.Open();
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetBaseException().Message, "服务器连接失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }

        private void txtServer_TextChanged(object sender, EventArgs e)
        {
            if (autoDict.ContainsKey(this.txtServer.Text.ToLower()))
            {
                this.txtUserName.Text = autoDict[this.txtServer.Text.ToLower()].Username;
                this.txtPassword.Text = autoDict[this.txtServer.Text.ToLower()].Password;
            }
        }





    }
}
